import re
from tkinter import *
import tkinter.messagebox as messagebox
import tkinter.scrolledtext
import time
from tkinter.filedialog import askdirectory
import os
import shutil

ver = "0.2"
title = "批量重命名工具 Version_" + str(ver)
samePath = '目标路径相同，请重新选择'
notSelectRules = '请选择处理规则'
notSelectPaths = '还没有选择路径'
checkPath = '检查路径'
processing = False


def selectSourcePath():
    path_ = askdirectory()
    # if sourcePath == objectPath: print('选择的目录一样，是否替换')
    print(path_)
    sourcePath.set(path_)


def selectObjectPath():
    path_ = askdirectory()
    print(path_)
    objectPath.set(path_)


root = Tk()
root.title(title)
root.geometry("880x400")
root.resizable(0, 0)
radio = IntVar()
sourcePath = StringVar()
objectPath = StringVar()
label = Label(root)
radio = IntVar()
inputRegular = StringVar()


def selection():
    selection = "选择规则 " + str(radio.get())
    print(selection)


def rules_1(sourcePath, objectPath):
    pattern = re.compile(
        r"[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]")
    for i in os.listdir(sourcePath.get()):
        # print(i)
        # print(re.search(pattern, i).group())
        filename_extension = '.' + str(i.split(".")[-1])
        try:
            # os.rename(os.path.join(sourcePath.get(), i), os.path.join(objectPath.get(), str(re.search(pattern, i).group()) + filename_extension))
            shutil.copyfile(os.path.join(sourcePath.get(), i), os.path.join(objectPath.get()+'/'+str(re.search(pattern, i).group())+ str(filename_extension)))
            # print(os.path.join(objectPath.get()+str(re.search(pattern, i).group()) + str(filename_extension)))
            # print('111', os.path.join(sourcePath.get(), i),
            #       '222',os.path.join(objectPath.get(), '333',str(re.search(pattern, i).group()) + filename_extension))
            showLogs('处理-' + str(i + '->' + str(str(re.search(pattern, i).group()) + filename_extension)))
        except BaseException as e:
            print(e)
            showLogs(str(e))


def process():
    print('选择的规则是', radio.get())
    print('源文件路径{}目标文件路径{}'.format(sourcePath.get(), objectPath.get()))
    if radio.get() == 0:  # 还没有选择处理规则
        print("请选择规则")
        showLogs(notSelectRules)
        messagebox.showinfo('提示', notSelectRules)
        return
    if sourcePath.get() == objectPath.get():  # 相同路径
        print('请检查路径')
        showLogs(samePath)
        messagebox.showinfo('提示', samePath)
        return
    if sourcePath.get() == '' or objectPath.get() == '':  # 空路径
        showLogs(notSelectPaths)
        print(notSelectPaths)
        messagebox.showinfo('提示', notSelectPaths)
        return
    file_list = os.listdir(sourcePath.get())
    showLogs('开始处理{}个文件'.format(len(file_list)))
    print('自定义', inputRegular.get())
    if radio.get() == 1:
        rules_1(sourcePath, objectPath)
    #
    # 这里根据正则规则进行处理
    # 然后定义一个日志输出


def showLogs(info):
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    scr.insert(END, t + '  ' + info + '\n')


Label(root, text="文件路径:").grid(row=0, column=0)
Entry(root, textvariable=sourcePath).grid(row=0, column=1)
Button(root, text="路径选择", command=selectSourcePath).grid(row=0, column=2)

# 目标路径

Label(root, text="目标路径:").grid(row=1, column=0)
Entry(root, textvariable=objectPath).grid(row=1, column=1)
Button(root, text="路径选择", command=selectObjectPath).grid(row=1, column=2)

Label(root, text="提取身份证号").grid(row=0, column=3, ipadx=120)
R1 = Radiobutton(root, text="规则A", variable=radio, value=1, command=selection)
R1.grid(row=0, column=4)
Label(root, text="正则规则B").grid(row=1, column=3, ipadx=120)
R2 = Radiobutton(root, text="规则B", variable=radio, value=2, command=selection)
R2.grid(row=1, column=4)

# Label(root, text="正则规则C").grid(row=2, column=3, ipadx=120)
Entry(root, show=None).grid(row=2, column=3, ipadx=120)
# inputRegular.get()
R3 = Radiobutton(root, text="规则C", variable=radio, value=3, command=selection)
R3.grid(row=2, column=4)

Button(root, text="开始处理", command=process).grid(row=3, column=3)

scr = tkinter.scrolledtext.ScrolledText(root, width=80, height=20, font=("宋体", 10))  # 滚动文本框（宽，高（这里的高应该是以行数为单位），字体样式）
# scr.grid(row=4, column=3)
scr.place(x=90, y=120)
root.mainloop()
